package com.shengqiang.cloud.oauth2.controller;

import com.shengqiang.cloud.oauth2.model.ProtectResource;
import com.shengqiang.cloud.oauth2.model.ResourceScope;
import com.shengqiang.cloud.oauth2.model.ResponseMsg;
import com.shengqiang.cloud.oauth2.service.OAuthAppResourceService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Administrator on 2017/12/11.
 */
@Controller
@RequestMapping("/oauth2App")
public class OAuthAppResourceController {

    @Autowired
    private OAuthAppResourceService service;

    @ApiOperation(value = "注册App到认证中心", notes = "")
    @ApiImplicitParam(name = "resource", value = "App资源信息", dataType = "ProtectResource", required = true)
    @ResponseBody
    @RequestMapping(value = "/registerApp", method = RequestMethod.POST)
    public ResponseMsg<String> registerAppResource(@RequestBody ProtectResource resource) {
        return service.registerResource(resource);
    }

    @ApiOperation(value = "更新认证中心中App资源信息", notes = "")
    @ApiImplicitParam(name = "resource", value = "App资源信息", dataType = "ProtectResource", required = true)
    @ResponseBody
    @RequestMapping(value = "/updateApp", method = {RequestMethod.POST, RequestMethod.PUT})
    public ResponseMsg<String> updateAppResource(@RequestBody ProtectResource resource) {
        return service.updateResource(resource);
    }

    @ApiOperation(value = "删除认证中心中注册App资源信息", notes = "")
    @ApiImplicitParam(name = "resourceId", value = "App资源Id", dataType = "String", required = true)
    @ResponseBody
    @RequestMapping(value = "/deleteApp", method = RequestMethod.GET)
    public ResponseMsg<String> deleteAppResource(@RequestBody String resourceId) {
        return service.deleteResource(resourceId);
    }

    @ApiOperation(value = "查询认证中心中注册的App资源列表", notes = "")
    @ResponseBody
    @RequestMapping(value = "/listApps", method = RequestMethod.GET)
    public ResponseMsg<List<ProtectResource>> queryApps() {
        return service.queryResources();
    }

    @ApiOperation(value = "添加注册的App资源Scope信息", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "resourceId", value = "App资源Id", dataType = "String", required = true),
            @ApiImplicitParam(name = "scope", value = "添加的Scope实体", dataType = "ResourceScope", required = true)
    })
    @ResponseBody
    @RequestMapping(value = "/addScope", method = RequestMethod.POST)
    public ResponseMsg<String> addAppScope(@RequestBody String resourceId, @RequestBody ResourceScope scope) {
        return service.addResourceScope(resourceId, scope);
    }

    @ApiOperation(value = "添加注册的App资源Scope信息", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "resourceId", value = "App资源Id", dataType = "String", required = true),
            @ApiImplicitParam(name = "scope", value = "修改的Scope实体", dataType = "ResourceScope", required = true)
    })
    @ResponseBody
    @RequestMapping(value = "/updateScope", method = {RequestMethod.POST, RequestMethod.PUT})
    public ResponseMsg<String> updateAppScope(@RequestBody String resourceId, @RequestBody ResourceScope scope) {
        return service.updateResourceScope(resourceId, scope);
    }

    @ApiOperation(value = "添加注册的App资源Scope信息", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "resourceId", value = "App资源Id", dataType = "String", required = true),
            @ApiImplicitParam(name = "scope", value = "修改的Scope实体", dataType = "ResourceScope", required = true)
    })
    @ResponseBody
    @RequestMapping(value = "/deleteScope", method = {RequestMethod.GET, RequestMethod.DELETE})
    public ResponseMsg<String> deleteAppScope(@RequestBody String resourceId, @RequestBody String scopeId) {
        return service.deleteScope(resourceId, scopeId);
    }
}
